package com.foxitcloud.webpdf.provider;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.foxitcloud.webpdf.utils.FXStringUtils;

/**
 * @author yxs
 * @version 1.0 Default Web Reader context storage class.
 */
@Scope("session")
@Service("iWebReaderStoreProvider")
public class DefaultWebReaderStoreProvider implements IWebReaderStoreProvider {
	private static final Logger log = Logger
			.getLogger(DefaultWebReaderStoreProvider.class);
	/**
	 * current session object.
	 */
	private HttpSession curSession = null;
	
	public DefaultWebReaderStoreProvider() {
		ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder
				.currentRequestAttributes();
		this.curSession = (attr == null) ? null : attr.getRequest()
				.getSession();
		
		if (this.curSession == null) {
			log.warn("Session is null in DefaultWebReaderStoreProvider");
		}
	}

	public void setPDFPassword(String key, String password) {
		String passwordSessionKey = key + "_psd";
		this.curSession.setAttribute(passwordSessionKey, password);
	}

	public String getPDFPassword(String key) {
		String docPsd = "";
		if (FXStringUtils.IsNullOrEmpty(key))
			return docPsd;
		String passwordSessionKey = key + "_psd";
		Object valueObj = this.curSession.getAttribute(passwordSessionKey);
		if (valueObj != null)
			docPsd = valueObj.toString().trim();
		return docPsd;
	}

	public void setFileForwardPassword(String key, String forwardPassword) {
		String passwordSessionKey = key + "_forwardPsd";
		this.curSession.setAttribute(passwordSessionKey, forwardPassword);
	}

	public String getFileForwardPassword(String key) {
		String frowardPsd = "";
		if (FXStringUtils.IsNullOrEmpty(key))
			return frowardPsd;
		String passwordSessionKey = key + "_forwardPsd";
		Object valueObj = this.curSession.getAttribute(passwordSessionKey);
		if (valueObj != null)
			frowardPsd = valueObj.toString().trim();
		return frowardPsd;
	}

	public void setFileForwardPermission(String key, long lPermission) {
		String szPemissionSessionKey = key + "_pemission";
		this.curSession.setAttribute(szPemissionSessionKey, lPermission);
	}

	public long getFileForwardPermission(String key) {
		long forwardPermission = 0;
		String pemissionSessionKey = key + "_pemission";
		Object valueObj = this.curSession.getAttribute(pemissionSessionKey);
		if (valueObj != null)
			forwardPermission = Long.parseLong(valueObj.toString().trim());
		return forwardPermission;
	}

	public boolean setDisplayPageRange(String key, int startPage, int endPage) {
		if (FXStringUtils.IsNullOrEmpty(key))
			return false;
		String startSessionKey = key + "_startpage";
		String endSessionKey = key + "_endpage";
		this.curSession.setAttribute(startSessionKey, startPage);
		this.curSession.setAttribute(endSessionKey, endPage);
		return true;
	}

	public boolean getDisplayPagesRange(String key, int[] displayPageRange) {
		if (FXStringUtils.IsNullOrEmpty(key) || displayPageRange.length != 2)
			return false;
		String startSessionKey = key + "_startpage";
		String endSessionKey = key + "_endpage";
		displayPageRange[0] = 0;
		displayPageRange[1] = -1;
		Object valueObj = this.curSession.getAttribute(startSessionKey);
		if (valueObj != null)
			displayPageRange[0] = Integer.valueOf(valueObj.toString().trim());
		valueObj = this.curSession.getAttribute(endSessionKey);
		if (valueObj != null)
			displayPageRange[1] = Integer.valueOf(valueObj.toString().trim());
		return true;
	}

	public boolean canPageDisplay(String strFileID, int nPageIndex) {
		int nStartPage = 0;
		int nEndPage = 0;
		int[] nStartAndEndPage = new int[2];
		if (!this.getDisplayPagesRange(strFileID, nStartAndEndPage))
			return false;
		nStartPage = nStartAndEndPage[0];
		nEndPage = nStartAndEndPage[1];
		if (nEndPage == -1 || nEndPage < nStartPage)
			return true;
		if (nPageIndex < nStartPage || nPageIndex > nEndPage)
			return false;
		else
			return true;
	}

	public void setPDFPasswordEncrypted(String key, boolean isEncryped) {
		String checkPsd_key = key + "_checkPsd";
		this.curSession.setAttribute(checkPsd_key, isEncryped);
	}

	public boolean isPDFPasswordEncrypted(String key) {
		boolean isEncryped = false;
		if (FXStringUtils.IsNullOrEmpty(key))
			return isEncryped;
		Object isEncryptedObj = this.curSession.getAttribute(key
				+ "_checkPsd");
		if (isEncryptedObj != null) {
			isEncryped = (Boolean) isEncryptedObj;
		}
		return isEncryped;
	}

	public boolean isManifestDataAccessed(String key) {
		Object isEncryptedObj = this.curSession.getAttribute(key
				+ "_checkPsd");
		if (isEncryptedObj == null) {
			return false;
		}
		return true;
	}

	@Override
	public void setTrail(String key, boolean isTrail) {
		String trailKey = key + "_trail";
		this.curSession.setAttribute(trailKey, isTrail);
	};
	
	@Override
	public boolean getTrail(String key){
		String trailKey = key + "_trail";
		return (boolean)this.curSession.getAttribute(trailKey);
	}
}
